package com.util;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.apache.logging.log4j.util.Strings.isBlank;

/**
 * @author chenhailong
 * @ClassName: SpringUtils
 * @Description: TODO
 * @date 2021/4/21 10:33
 * @Version 1.0
 */
public class StringUtils {


    public static void main(String[] args) {
        // 取字符串
//        String string = getCharater("中国人abcd", 4);
//        System.out.println("第几个为:" + string);
//
//        boolean phone = isPhoneNumber("29212345678");
//        System.out.println("是否为手机号:" + phone);
//
//        // ip 地址校验
//        String regex = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";
//        String ip = "255.255.255.255";
//        boolean matches1 = Pattern.matches(regex, ip);
//        System.out.println("直接使用工具:" + matches1);
//        // 字符串自带正则匹配
//        boolean matches = ip.matches(regex);
//        System.out.println("极限测试str结果更准确:" + matches);
//
//        System.out.println("是否存在中文:" + isContainChinese("我abs123s"));



        String te = "{replacecontent}";  //字符串替换的内容
        System.out.println(resplace(te,"https://0.0.0.0:8080/abc-test-reg/test/index?p=0123456&trigger=2&param" +
                "=0d243b2-cd56-11e-abu9-05692e327ff","p=(.*?)&tr"));

    }

    public static String buildUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * @param str str
     * @return response response
     * @Author chl
     * @Description 判断是否手机号
     * @Date 15:53 2021/3/18
     **/
    public static boolean isPhone(String str) {
        String regEx = "^(\\+?86-?)?1(3|4|7|5|8)[0-9]{9}$";
        if (Pattern.matches(regEx, str)) {
            return true;
        }
        return false;
    }

    /**
     * @param str str
     * @return response response
     * @Author chl
     * @Description 判断手机号只为1开头的11位数字
     * @Date 15:53 2021/3/18
     **/
    public static boolean isPhoneNumber(String str) {
        String regEx = "^(\\+?86-?)?1[0-9]{10}$";
        if (Pattern.matches(regEx, str)) {
            return true;
        }
        return false;
    }

    /**
     * 是否是数字
     * <pre>
     * StringUtil.isNumeric(null)   = false
     * StringUtil.isNumeric("")     = false
     * StringUtil.isNumeric("  ")   = false
     * StringUtil.isNumeric("12 3") = false
     * StringUtil.isNumeric("ab2c") = false
     * StringUtil.isNumeric("12-3") = false
     * StringUtil.isNumeric("12.3.3") = false
     * StringUtil.isNumeric("123")  = true
     * StringUtil.isNumeric("-12.3") = true
     * StringUtil.isNumeric("+12.3") = true
     * StringUtil.isNumeric("00000.3") = true
     * StringUtil.isNumeric("00000.") = true
     * </pre>
     *
     * @param str
     *
     * @return
     */
    public static boolean isNumeric(String str) {
        String regEx = "[-\\+]??[0-9]+?\\.??[0-9]*?";
        if (Pattern.matches(regEx, str)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串中是否包含中文
     * @param str
     * 待校验字符串
     * @return 是否为中文
     * @warn 不能校验是否为中文标点符号
     */
    public static boolean isContainChinese(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (isChinese(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    /**
     * @Description 是否汉字
     * @param c c
     * @return response response
     * @Author chl
     * @Date 10:49 2021/4/21
     **/
    private static boolean isChinese(char c) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(c + "");
        if (m.find())
            return true;
        return false;
    }

    /**
     * @param position 第几位
     * @param str      要取的字符串
     * @return response response
     * @Author chl
     * @Description 取字符串的第几位
     * @Date 10:57 2021/4/21
     **/
    public static String getCharater(String str, int position) {
//        String c = "中国人abcd";
        int index = -1;
        int now = 0;
        for (int i = 0; i < str.length(); i++) {
            now = i;
            if (isChinese(str.charAt(i))) {
                if (++index == position)
                    break;
            } else {
                if (++index == position)
                    break;
            }
        }
        return String.valueOf(str.charAt(now));
    }

    /**
     * @param str         要替换内容
     * @param resplaceStr 被替换内容
     * @param reg 正则
     * @return response response
     * @Author chl
     * @Description 正则替换字符串中间部分
     * @Date 19:31 2024/2/2
     **/
    public static String resplace(String str, String resplaceStr, String reg) {

        StringBuilder operatorStr = new StringBuilder(resplaceStr);
        Pattern p = Pattern.compile(reg);
        Matcher m = p.matcher(operatorStr);
        if (m.find()) {
            //使用分组进行替换
            operatorStr.replace(m.start(1), m.end(1), str);
        }
        return operatorStr.toString();
    }

    /**
     * @param str         被替换内容
     * @param strIndex    开始下标
     * @param endIndex    结束下标
     * @return resplace   替换字段
     * @Author chl
     * @Description 正则替换字符串中间部分
     * @Date 19:31 2024/2/2
     **/
    public static String resplace(int strIndex, int endIndex, String resplace, String str) {
        StringBuilder operatorStr = new StringBuilder(str);
        StringBuilder replace = operatorStr.replace(strIndex, endIndex, resplace);
        return replace.toString();
    }



    /**
     * 隐藏手机号中间4位
     *
     * @param mobile
     * @return
     */
    public static String halfMobileCover(String mobile) {
        if (isBlank(mobile)) {
            return "";
        }
        if (mobile.length() != 11) {
            return mobile;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(mobile.substring(0, 3));
        sb.append("****");
        sb.append(mobile.substring(7));
        return sb.toString();
    }

    /**
     * 隐藏身份证中间
     *
     * @param idcard
     * @return
     */
    public static String halfIDCardCover(String idcard) {
        if (isBlank(idcard)) {
            return "";
        }
        if (idcard.length() < 9) {
            return halfNameCover(idcard);
        }
        StringBuilder sb = new StringBuilder(idcard);
        String s = "*";
        for (int i = 0; i < idcard.length()-3; i++){
            if (i > 4) {
                sb.replace(i - 1, i, s);
            }
        }
        return sb.toString();
    }

    /**
     * 隐藏名字中间
     *
     * @param name
     * @return
     */
    public static String halfNameCover(String name) {
        if (isBlank(name)) {
            return "";
        }
        if (name.length() == 1) {
            return name;
        }

        StringBuilder sb = new StringBuilder(name);
        String s = "*";
        for (int i = 1; i < name.length(); i++){
            sb.replace(i - 1, i, s);
        }

        return sb.toString();
    }

    /**
     * 判断非负数的整数或者携带一位或者两位的小数
     *
     * @function:
     * @param obj
     * @return boolean
     * @exception
     */
    public static boolean judgeTwoDecimal(Object obj){
        boolean flag = false;
        try {
            if(obj != null){
                String source = obj.toString();
                // 判断是否是整数或者是携带一位或者两位的小数
                Pattern pattern = Pattern.compile("^[+]?([0-9]+(.[0-9]{1,2})?)$");
                if(pattern.matcher(source).matches()){
                    flag = true;
                }
            }
        } catch (Exception e) {

        }
        return flag;
    }


}
